package com.turing.data.scripts.contract.filter

import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils
import com.turing.data.engine.utils.RegexExtract

class OperateMethod1_30172_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){


        String openStyle1 = /.*开放式.*/
        String openStyle2 = /.*开放.*/
        String closeStyle = /.*封闭式.*/
        String closeStyle_2 = /.*封闭运作.*/
        String operate = /.*运作.*/

        def textPathMap = dependParams.textPathMap

        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()

        try{
            if(trueValueSet.size()==0){

                String REGEX = /运作方式本计划.{1,10}运作/
                println dependParams.JSONTextMap.size()
                dependParams.JSONTextMap.forEach(
                        { childTaskSn, jsonText ->
                            RegexExtract.doRuleExtract(REGEX,childTaskSn, dependParams, jsonText, trueValueSet)
                        }
                )

                if(trueValueSet.size()>0){
                    def childTaskSn = trueValueSet[0].childTaskSn
                    def rawPath = textPathMap.get(childTaskSn)
                    def rawText = FileUtils.loadJson(rawPath)
                    def JSONTextMap = dependParams.JSONTextMap
                    def fileTextStr = ""
                    fileTextStr = JSONTextMap.get(childTaskSn)

                    def text = trueValueSet[0].text
                    def startOffset = text.indexOf("本计划")
                    def endOffset = 0
                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(trueValueSet[0])
                    objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                    trueValueSet.clear()
                    trueValueSet.add(objectNew)
                    return trueValueSet
                }
            }
        }
        catch (Exception e){
            e.printStackTrace()
        }
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                if(StringUtils.isEmpty(resultVOs.get(i).childTaskSn)){
                    return  resultVOs
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def resultVO = resultVOs[i]
                String text = resultVO.text
                def start = resultVO.start
                def end = resultVO.end
                def expand = 0

                while( expand < 20 && start > expand ){
                    def expandString = rawText.substring(start-expand,end)
                    if( expandString =~ operate ){
                        trueValueSet.add(resultVOs.get(i))
                        return trueValueSet
                    }
                    expand ++
                }
            }
        }

        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                if(StringUtils.isEmpty(resultVOs.get(i).childTaskSn)){
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def resultVO = resultVOs[i]
                String text = resultVO.text

                if (text =~ openStyle1  || text =~ openStyle2 || text =~ closeStyle){
                    trueValueSet.add(resultVOs.get(i))
                    return trueValueSet
                }
            }
        }

        def JSONTextMap = dependParams.JSONTextMap

        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def resultVO = resultVOs[i]
                def text = resultVO.text
                def childTaskSn = resultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == "" || StringUtils.isEmpty(text)){
                    trueValueSet.add(resultVOs.get(0))
                    return trueValueSet
                }
                def start = resultVO.start
                def end = resultVO.end

                def rawPath = textPathMap.get(resultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def fileTextStr = ""
                fileTextStr = JSONTextMap.get(childTaskSn)
                def expandOffset = 0
                while( expandOffset < 150 && rawText.length() > expandOffset + end ){
                    def expandStr = rawText.substring(start,end+expandOffset)
                    if( expandStr =~ closeStyle_2 ){
                        def startOffset = expandStr.length() - 4
                        TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVO)
                        ObjectNew = TextUtils.correctPositionInfoByOffset(ObjectNew, startOffset, expandOffset, fileTextStr, rawText)
                        ObjectNew.text = rawText.substring(start + startOffset, end + expandOffset )
                        ObjectNew.businessValue = rawText.substring(start + startOffset, end + expandOffset)
                        trueValueSet.add(ObjectNew)
                        printf trueValueSet.toString()
                        return trueValueSet
                    }
                    expandOffset ++
                }

            }
        }

        return  resultVOs

    }

}
